/**
 * Copyright 2014 
 * Author：liwen
 * 创建日期：2014-11-10
 */
package com.acoreful.acf2admin.web.utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

public class AjaxUtil {

	public static ResponseEntity<String> jsonResult(String result){
		HttpHeaders resHeaders = new HttpHeaders();
		resHeaders.set("Content-Type", "application/json;charset=utf-8");
		return new ResponseEntity<String>(result, resHeaders, HttpStatus.OK);
	}
	
	public static ResponseEntity<String> error(String errorMsg){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("status", false);
		map.put("message", errorMsg);
		map.put("noPri", "N");
		return jsonResult(JSON.toJSONString(map));
	}
	
	public static ResponseEntity<String> errorNoPri(){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("status", false);
		map.put("message", "您没有操作权限");
		map.put("noPri", "Y");
		return jsonResult(JSON.toJSONString(map));
	}
	
	public static ResponseEntity<String> success(){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("status", true);
		map.put("message", "");
		map.put("noPri", "N");
		return jsonResult(JSON.toJSONString(map));
	}
	
	public static ResponseEntity<String> successDatas(Object obj,String ...params){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("status", true);
		map.put("data", obj);
		map.put("message", "");
		map.put("noPri", "N");
		SimplePropertyPreFilter filter=new SimplePropertyPreFilter();//构造方法里，也可直接转需要序列号的属性
	    if(params!=null){
	    	filter.getExcludes().addAll(Arrays.asList(params));
	    }
		return jsonResult(JSON.toJSONString(map,filter));
	}
	public static ResponseEntity<String> successPageDatas(Object obj,String curentPage,long totalSize){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("status", true);
		map.put("data", obj);
		map.put("message", "");
		map.put("curentPage", curentPage);
		map.put("totalSize", totalSize);
		map.put("noPri", "N");
		return jsonResult(JSON.toJSONString(map));
	}
}
